﻿using DGIS.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DGIS.Map.UserControls
{
    /// <summary>
    /// DownloadControl.xaml 的交互逻辑
    /// </summary>
    public partial class DownloadControl : UserControl
    {
        public DownloadControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (AppConstant.DownloadProgressList != null)
                this.ProgressListbox.ItemsSource = AppConstant.DownloadProgressList;
        }

        private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           if (this.MainTabControl.SelectedIndex == 1)
              ShowCompleteTasks();
        }

        /// <summary>
        /// 显示已经完成任务集合
        /// </summary>
        private void ShowCompleteTasks() {            
            DGIS.Control.DownloadControl control = new Control.DownloadControl();
            List<DownloadFile> items=control.GetCompleteTasks();
            foreach (var item in items)
            {
                item.IconSource = GetDownloadFileIco(item.SavePath);                
            }

            ObservableCollection<DownloadFile> downloadList = new ObservableCollection<DownloadFile>(items);
            this.CompleteFilesListbox.ItemsSource = downloadList;
        }

        /// <summary>
        /// 获取文件扩展名和图标
        /// </summary>
        /// <param name="extension"></param>
        /// <param name="icon"></param>
        /// <param name="gsDataSetType"></param>
        private BitmapImage GetDownloadFileIco(string fileName)
        {
            BitmapImage icon;
            string extension = System.IO.Path.GetExtension(fileName);
            switch (extension)
            {
                case ".hdf":
                    icon = new BitmapImage(new Uri(string.Format("{0}Images\\FileIcon\\hdf.png", AppDomain.CurrentDomain.BaseDirectory)));
                    break;
                case ".gz":
                    icon = new BitmapImage(new Uri(string.Format("{0}Images\\FileIcon\\zip.png", AppDomain.CurrentDomain.BaseDirectory)));
                    break;
                case ".tgz":
                    icon = new BitmapImage(new Uri(string.Format("{0}Images\\FileIcon\\tgz.png", AppDomain.CurrentDomain.BaseDirectory)));
                    break;
                case ".tif":
                    icon = new BitmapImage(new Uri(string.Format("{0}Images\\FileIcon\\bmp.png", AppDomain.CurrentDomain.BaseDirectory)));
                    break;
                default:
                    icon = new BitmapImage(new Uri(string.Format("{0}Images\\FileIcon\\zip.png", AppDomain.CurrentDomain.BaseDirectory)));
                    break;
            }

            return icon;
        }
    }
}
